﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.WebControls;

namespace DR5.SPExtend
{
    public static class SPPrincipalExtension
    {
        /// <summary>
        /// Check if user exist in collection
        /// </summary>
        /// <param name="usrColl"></param>
        /// <param name="usr"></param>
        /// <returns></returns>
        public static bool Exist(this SPUserCollection usrColl, SPUser usr)
        {
            foreach (SPUser str in usrColl)
            {
                if (string.Compare(str.LoginName, usr.LoginName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return true;
                }
            }
            return false;
        }

        public static bool Exist(this SPUser[] usrColl, SPUser usr)
        {
            foreach (SPUser u in usrColl)
            {
                if (string.Compare(u.LoginName, usr.LoginName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Check if user exist in Group
        /// </summary>
        /// <param name="grp"></param>
        /// <param name="usr"></param>
        /// <returns></returns>
        public static bool UserExist(this SPGroup grp, SPUser usr)
        {
            foreach (SPUser str in grp.Users)
            {
                if (str.LoginName == usr.LoginName)
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsAllAuthenticatedUserInGroup(this SPGroup group)
        {
            SPUserCollection _usrColl = group.Users;
            foreach (SPUser usr in _usrColl)
            {
                if (usr.LoginName == "c:0(.s|true")
                {
                    return true;
                }
            }
            return false;
        }

        public static string CommaSepratedUsersEmail(this SPUser[] usrColl)
        {
            string str = string.Empty;

            if (usrColl != null)
            {
                bool firstTime = true;

                foreach (SPUser obj in usrColl)
                {
                    if (firstTime)
                    {
                        str = obj.Email;
                        firstTime = false;
                    }
                    else
                    {
                        string email = obj.Email;
                        if (!string.IsNullOrEmpty(email))
                        {
                            str += ",  " + email;
                        }
                    }
                }
            }
            return str;
        }

        public static string CommaSepratedUsersEmail(this SPUserCollection usrColl)
        {
            string str = string.Empty;
            bool firstTime = true;

            foreach (SPUser obj in usrColl)
            {
                if (firstTime)
                {
                    str = obj.Email;
                    firstTime = false;
                }
                else
                {
                    string email = obj.Email;
                    if (!string.IsNullOrEmpty(email))
                    {
                        str += ",  " + email;
                    }
                }
            }
            return str;
        }

        public static string CommaSepratedUsersEmail(this List<SPUser> usrColl)
        {
            string str = string.Empty;

            if (usrColl != null)
            {
                bool firstTime = true;

                foreach (SPUser obj in usrColl)
                {
                    if (firstTime)
                    {
                        str = obj.Email;
                        firstTime = false;
                    }
                    else
                    {
                        string email = obj.Email;
                        if (!string.IsNullOrEmpty(email))
                        {
                            str += ",  " + email;
                        }
                    }
                }
            }
            return str;
        }

        public static void AddUser(this PeopleEditor pe, params SPUser[] usr)
        {
            System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();

            for (int i = 0; i < usr.Length; i++)
            {
                PickerEntity entity = new PickerEntity();
                entity.Key = usr[i].LoginName;
                entityArrayList.Add(entity);
            }
            pe.UpdateEntities(entityArrayList);
        }

        public static void Remove(this SPFieldUserValueCollection usrColl, SPUser usr)
        {
            SPFieldUserValue toRemove = null;
            foreach (SPFieldUserValue value in usrColl)
            {
                if (value.LookupId == usr.ID)
                {
                    toRemove = value;
                    break;
                }
            }
            if (toRemove != null)
            {
                usrColl.Remove(toRemove);
            }
        }

        public static bool Exist(this SPRoleDefinitionCollection roleDefs, string roleName)
        {
            foreach (SPRoleDefinition item in roleDefs)
            {
                if (item.Name == roleName)
                {
                    return true;
                }
            }
            return false;
        }

        public static string CommaSeparatedRoleDefination(this SPRoleDefinitionBindingCollection coll)
        {
            string str = string.Empty;
            foreach (SPRoleDefinition obj in coll)
            {
                str += obj.Name + ",  ";
            }
            return str;
        }
    }
}
